﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Globalization;

namespace Gmantis.Controls
{
    [TemplateVisualState(Name = "Full", GroupName = "PreviewStates"), TemplateVisualState(Name = "Disabled", GroupName = "CommonStates"), TemplateVisualState(Name = "Preview", GroupName = "PreviewStates"), TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
    public class ListViewerItem : ContentControl
    {
        // Fields
        internal bool _isLoaded;
        public static readonly DependencyProperty PreviewContentProperty = DependencyProperty.Register("PreviewContent", typeof(object), typeof(ListViewerItem), null);
        public static readonly DependencyProperty PreviewContentTemplateProperty = DependencyProperty.Register("PreviewContentTemplate", typeof(DataTemplate), typeof(ListViewerItem), null);
        public static readonly DependencyProperty StateProperty = DependencyProperty.Register("State", typeof(ListBoxItemState), typeof(ListViewerItem), new PropertyMetadata(ListBoxItemState.Full, new PropertyChangedCallback(ListViewerItem.OnStatePropertyChanged)));

        // Methods
        public ListViewerItem()
        {
            RoutedEventHandler handler = null;
            DependencyPropertyChangedEventHandler handler2 = null;
            base.DefaultStyleKey = typeof(ListViewerItem);
            if (handler == null)
            {
                handler = delegate(object param0, System.Windows.RoutedEventArgs param1)
                {
                    this.ChangeVisualStatePreview(false);
                    this.ChangeVisualStateCommon(false);
                };
            }
            base.Loaded += handler;
            if (handler2 == null)
                handler2 = (s, a) => this.ChangeVisualStateCommon(true);

            base.IsEnabledChanged += handler2;
        }

        protected void ChangeVisualStateCommon(bool useTransitions)
        {
            if (!base.IsEnabled)
            {
                VisualStateHelper.GoToState(this, "Disabled", useTransitions);
            }
            if (base.IsEnabled)
            {
                VisualStateHelper.GoToState(this, "Normal", useTransitions);
            }
        }

        protected void ChangeVisualStatePreview(bool useTransitions)
        {
            if (this.State == ListBoxItemState.Full)
            {
                VisualStateHelper.GoToState(this, "Full", useTransitions);
            }
            if (this.State == ListBoxItemState.Preview)
            {
                VisualStateHelper.GoToState(this, "Preview", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to ListViewerItem.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.ChangeVisualStatePreview(false);
                this.ChangeVisualStateCommon(false);
            }
        }

        private static void OnStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ListViewerItem).ChangeVisualStatePreview(true);
        }

        // Properties
        internal bool IsPrepared { get; set; }

        public object PreviewContent
        {
            get
            {
                return base.GetValue(PreviewContentProperty);
            }
            set
            {
                base.SetValue(PreviewContentProperty, value);
            }
        }

        public DataTemplate PreviewContentTemplate
        {
            get
            {
                return (DataTemplate)base.GetValue(PreviewContentTemplateProperty);
            }
            set
            {
                base.SetValue(PreviewContentTemplateProperty, value);
            }
        }

        public ListBoxItemState State
        {
            get
            {
                return (ListBoxItemState)base.GetValue(StateProperty);
            }
            set
            {
                base.SetValue(StateProperty, value);
            }
        }
    }

    public enum ListBoxItemState
    {
        Preview,
        Full
    }
}
